﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SemPlayGen.Repository.Repositories;
using SemPlayGen.DataModel;
using SemPlayGen.Logic;
using System.Drawing;

namespace SemPlayGen.Website
{
    public partial class UploadSong : System.Web.UI.Page
    {
        ArtistTypesRepository artistTypesRepo = new ArtistTypesRepository();
        GenresRepository genresRepo = new GenresRepository();
        MusicTypesRepository musicTypesRepo = new MusicTypesRepository();
        SongsRepository songsRepo = new SongsRepository();
        SignalsRepository signalsRepo = new SignalsRepository();
        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlArtistType.DataSource = artistTypesRepo.GetAll();
                ddlArtistType.DataBind();

                ddlGenre.DataSource = genresRepo.GetAll();
                ddlGenre.DataBind();

                ddlMusicType.DataSource = musicTypesRepo.GetAll();
                ddlMusicType.DataBind();
                
                ddlSignal.DataSource = signalsRepo.GetAll();
                ddlSignal.DataBind();
            }
        }

        protected void ddl_DataBound(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;

            if (ddl.Items.FindByValue("0") == null)
                ddl.Items.Insert(0, new ListItem("< Изберете >", "0"));
            ddl.SelectedIndex = 0;
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtArtistName.Text = txtSongName.Text = "";
            ddlArtistType.SelectedIndex = ddlGenre.SelectedIndex = ddlMusicType.SelectedIndex = ddlSignal.SelectedIndex = 0;
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            Songs song = new Songs();

            song.ArtistName = txtArtistName.Text;
            song.SongName = txtSongName.Text;
            song.FilenamePath = "";
            song.ArtistTypeID = Convert.ToInt32(ddlArtistType.SelectedValue);
            song.GenreID = Convert.ToInt32(ddlGenre.SelectedValue);
            song.MusicTypeID = Convert.ToInt32(ddlMusicType.SelectedValue);
            song.SignalID = Convert.ToInt32(ddlSignal.SelectedValue);
            song.HasInstrument = true;
            song.HasLyrics = chcWithText.Checked;

            try
            {
                if (songsRepo.Insert(song))
                {
                    Semantics.InsertToRDF(song);

                    lblStatus.Text = "Успешно внесена песната.";
                    lblStatus.ForeColor = Color.Black;

                    btnReset_Click(sender, e);  //reset form                    
                }
                else
                {
                    lblStatus.Text = "Се појави грешка. Песната не е внесена.";
                    lblStatus.ForeColor = Color.Red;
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = "[Error]: " + ex.ToString();
                lblStatus.ForeColor = Color.Red;
            }
        }
    }
}